clear
clear matrix
clear mata
set more off, perm
set matsize 11000
set maxvar 30000
cd /Users/zachbrown/Projects/PriceTransparency/Data/
global output /Users/zachbrown/Projects/PriceTransparency/Output

// Open visit dataset
use clean/radiology_visit_3.dta, clear

// Make treatment indicator
gen post = (ym>=ym(2007,3))
gen web_post = (visit_on_web==1 & post==1)

// Labels
label var amt_paid "Paid amount"
label var amt_prepaid "FFS equivalent (capitation)"
label var amt_copay "Copayment"
label var amt_coin "Coinsurance"
label var amt_deduct "Deductible"
label var amt_member_oop "Total out-of-pocket cost"
label var amt_billed "Charge amount"

// Panel A
local sum_vars "amt_copay amt_coins amt_deduct amt_member_oop"
local n_vars: word count `sum_vars'
disp `n_vars'

estpost summarize `sum_vars' if post==0 & visit_on_web==1
quiet summ n_procs if post==0 & visit_on_web==1
estadd scalar n_procs = r(sum), replace
eststo summA

estpost summarize `sum_vars' if post==1 & visit_on_web==1
quiet summ n_procs if post==1 & visit_on_web==1
estadd scalar n_procs = r(sum), replace
eststo summB

estpost summarize `sum_vars' if post==0 & visit_on_web==0
quiet summ n_procs if post==0 & visit_on_web==0
estadd scalar n_procs = r(sum), replace
eststo summC

estpost summarize `sum_vars' if post==1 & visit_on_web==0
quiet summ n_procs if post==1 & visit_on_web==0
estadd scalar n_procs = r(sum), replace
eststo summD

esttab summA summB summC summD using $output/visits_summary_a.tex, frag ///
	cells("mean(fmt(%8.1fc) label( ) ) sd(fmt(%8.1fc) label( ) )" ) ///
	stats() /// 
	delimiter(&) end(\\) nolines ///
 	noobs collabels(none) booktabs gaps nonumber label  nonote replace nomtitles nodepvars 
	
// Panel B
local sum_vars "amt_paid"
local n_vars: word count `sum_vars'
disp `n_vars'

estpost summarize `sum_vars' if post==0 & visit_on_web==1
quiet summ n_procs if post==0 & visit_on_web==1
estadd scalar n_procs = r(sum), replace
eststo summA

estpost summarize `sum_vars' if post==1 & visit_on_web==1
quiet summ n_procs if post==1 & visit_on_web==1
estadd scalar n_procs = r(sum), replace
eststo summB

estpost summarize `sum_vars' if post==0 & visit_on_web==0
quiet summ n_procs if post==0 & visit_on_web==0
estadd scalar n_procs = r(sum), replace
eststo summC

estpost summarize `sum_vars' if post==1 & visit_on_web==0
quiet summ n_procs if post==1 & visit_on_web==0
estadd scalar n_procs = r(sum), replace
eststo summD

esttab summA summB summC summD using $output/visits_summary_b.tex, frag ///
	cells("mean(fmt(%8.1fc) label( ) ) sd(fmt(%8.1fc) label( ) )" ) ///
	stats() /// 
	delimiter(&) end(\\) nolines ///
 	noobs collabels(none) booktabs gaps nonumber label  nonote replace nomtitles nodepvars 
	
// Panel C
local sum_vars "amt_allowed amt_billed"
local n_vars: word count `sum_vars'
disp `n_vars'

estpost summarize `sum_vars' if post==0 & visit_on_web==1
quiet summ n_procs if post==0 & visit_on_web==1
estadd scalar n_procs = r(sum), replace
eststo summA

estpost summarize `sum_vars' if post==1 & visit_on_web==1
quiet summ n_procs if post==1 & visit_on_web==1
estadd scalar n_procs = r(sum), replace
eststo summB

estpost summarize `sum_vars' if post==0 & visit_on_web==0
quiet summ n_procs if post==0 & visit_on_web==0
estadd scalar n_procs = r(sum), replace
eststo summC

estpost summarize `sum_vars' if post==1 & visit_on_web==0
quiet summ n_procs if post==1 & visit_on_web==0
estadd scalar n_procs = r(sum), replace
eststo summD

esttab summA summB summC summD using $output/visits_summary_c.tex, frag ///
	cells("mean(fmt(%8.1fc) label( ) ) sd(fmt(%8.1fc) label( ) )" ) ///
	stats() /// 
	delimiter(&) end(\\) nolines ///
 	noobs collabels(none) booktabs gaps nonumber label  nonote replace nomtitles nodepvars 


	
// Number of obs
esttab summA summB summC summD using $output/visits_summary_d.tex, frag ///
	drop(*) cells("mean(fmt(%8.1fc) label( ) ) sd(fmt(%8.1fc) label( ) )" ) ///
	stats(N n_procs, fmt(%12.0fc %12.0fc) labels("Observations (Visits)" "Total Procedures")  layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}")) ///
	varwidth(20) modelwidth(12) delimiter(&) end(\\) collabels(none) nolines ///
	nonumber label  nonote replace nomtitles nodepvars noisily 

